import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

public class Bomb {
    public int x, y;
    public int width = 30, height = 30;
    public int speed;
    public boolean active = true;
    public boolean exploding = false;
    public long explodeStartTime;

    private Image bombImage;
    private Image boomImage;

    public Bomb(int startX, int startY, int speed) {
        this.x = startX;
        this.y = startY;
        this.speed = speed;

        try {
            bombImage = new Image(getClass().getResourceAsStream("/images/bomb.png"));
            boomImage = new Image(getClass().getResourceAsStream("/images/boom.png"));
        } catch (Exception e) {
            System.err.println("加载炸弹图像失败: " + e.getMessage());
        }
    }

    public void update() {
        if (!active) return;

        if (exploding) {
            if (System.currentTimeMillis() - explodeStartTime > 500) {
                active = false;
            }
        } else {
            int oldX = x;
            x += (int)(speed * GameConstants.GAME_SPEED);

            if (x > GameConstants.getScreenWidth()) {
                active = false;
            }
        }
    }

    public void explode() {
        exploding = true;
        explodeStartTime = System.currentTimeMillis();
        width = height = 60; // 爆炸效果更大
    }

    public void render(GraphicsContext gc) {
        if (!active) return;

        // 添加调试输出
        System.out.println("渲染炸弹 at (" + x + "," + y + ") " +
                (exploding ? "爆炸状态" : "飞行状态"));

        if (exploding) {
            gc.drawImage(boomImage, x - 15, y - 15, width, height);
        } else {
            gc.drawImage(bombImage, x, y, width, height);
        }
    }
}